package group

import (
	"gddgame.cc/galaxy/common/dsl/game"
	"gddgame.cc/galaxy/utils/orm"
)

const DefaultGroup = 0

type GroupData struct {
	GroupMap map[int]*SettingGroup
	Groups   []SettingGroup

	RelationGroupMap map[int]*SettingRelationGroup
	RelationGroups   []SettingRelationGroup

	RelationMap map[string][]*SettingRelationGroup

	RelationGroupList        []*RelationGroup
	RelationGroupManagerList []*RelationGroupManager

	groupModel         *orm.Model
	relationGroupModel *orm.Model
}

func GetGroupData(env *game.Environment) (*GroupData, error) {
	var err error
	data := &GroupData{}

	if data.groupModel, data.Groups, data.GroupMap, err = getGroupData(env); err != nil {
		return nil, err
	}
	if data.relationGroupModel, data.RelationGroups, data.RelationGroupMap, err = getRelationGroupData(env); err != nil {
		return nil, err
	}

	groupProvider := &game.DataProvider{
		Name:  "group",
		Array: true,
		Get: func() (i interface{}, err error) {
			return data.Groups, nil
		},
	}
	relationGroupProvider := &game.DataProvider{
		Name:     "relationGroup",
		Array:    true,
		Language: true,
		Get: func() (i interface{}, err error) {
			return data.RelationGroupList, nil
		},
	}
	relationGroupManagerProvider := &game.DataProvider{
		Name:    "relationGroupManager",
		Array:   true,
		Manager: true,
		Get: func() (i interface{}, err error) {
			return data.RelationGroupManagerList, nil
		},
	}
	data.RelationMap = make(map[string][]*SettingRelationGroup)
	for index := range data.RelationGroups {
		item := data.RelationGroups[index]
		if _, ok := data.RelationMap[item.Type]; !ok {
			data.RelationMap[item.Type] = []*SettingRelationGroup{}
		}
		data.RelationMap[item.Type] = append(data.RelationMap[item.Type], &data.RelationGroups[index])
	}
	data.RelationGroupList = make([]*RelationGroup, len(data.RelationGroups))
	for index := range data.RelationGroups {
		data.RelationGroupList[index] = NewRelationGroup(relationGroupProvider, &data.RelationGroups[index])
	}
	//fmt.Println(data.RelationGroupList)
	data.RelationGroupManagerList = make([]*RelationGroupManager, len(data.RelationGroups))
	for index := range data.RelationGroups {
		data.RelationGroupManagerList[index] = NewRelationGroupManager(relationGroupProvider, &data.RelationGroups[index])
	}

	env.RegisterData(groupProvider)
	env.RegisterData(relationGroupProvider)
	env.RegisterData(relationGroupManagerProvider)
	return data, nil
}

func getGroupData(env *game.Environment) (*orm.Model, []SettingGroup, map[int]*SettingGroup, error) {
	var err error
	// group
	var groups []SettingGroup

	model, err := env.GetModel(SettingGroupName)
	if err != nil {
		return nil, nil, nil, err
	}
	_, err = model.Query().Find(&groups)
	if err != nil {
		return nil, nil, nil, err
	}
	groupMap := make(map[int]*SettingGroup, len(groups))
	for k := range groups {
		groupMap[groups[k].Id] = &groups[k]
	}
	return model, groups, groupMap, nil
}

func getRelationGroupData(env *game.Environment) (*orm.Model, []SettingRelationGroup, map[int]*SettingRelationGroup, error) {
	var err error
	// group
	var groups []SettingRelationGroup

	model, err := env.GetModel(SettingRelationGroupName)
	if err != nil {
		return nil, nil, nil, err
	}
	_, err = model.Query().Find(&groups)
	if err != nil {
		return nil, nil, nil, err
	}
	groupMap := make(map[int]*SettingRelationGroup, len(groups))
	for k := range groups {
		groupMap[groups[k].Id] = &groups[k]
	}
	return model, groups, groupMap, nil
}
